#include "conversion.h"


void Conversion::doNoize(double eps)
{
#ifdef USE_MPI || USE_SHMEM
    complexd *new_vec = new complexd [4];
    double phi;

    if (rank == root)
        phi = eps * normal_dis_gen();
#ifdef USE_MPI
    MPI_Bcast(&phi, 1, MPI_DOUBLE, root, MPI_COMM_WORLD);
#elif USE_SHMEM
    ;
#endif

    new_vec[0] = vec[0] * cos(phi) - vec[1] * sin(phi);
    new_vec[1] = vec[0] * sin(phi) + vec[1] * cos(phi);
    new_vec[2] = vec[2] * cos(phi) - vec[3] * sin(phi);
    new_vec[3] = vec[2] * sin(phi) + vec[3] * cos(phi);

    vec[0] = new_vec[0];
    vec[1] = new_vec[1];
    vec[2] = new_vec[2];
    vec[3] = new_vec[3];
#endif
}


Conversion::Conversion(int n, complexd* vector)
{
    length = n;
    vec = new complexd [n];
    for (int i = 0; i < n; i++)
        vec[i] = vector[i];
}

Conversion::Conversion(const Conversion &conversion)
{
    length = conversion.length;
    vec = new complexd [length];
    for (int i = 0; i < length; i++)
        vec[i] = conversion.vec[i];
}

Conversion::~Conversion()
{
    delete [] vec;
}

complexd Conversion::operator[](int k)
{
    if (k >= length)
        cerr << "Error" << endl;
    return vec[k];
}

ostream& operator<<(ostream &stream, Conversion &con)
{
    for (int i = 0; i < con.length; i++)
        stream << con.vec[i];
    stream << endl;
    return stream;
}


double Conversion::normal_dis_gen()
{
    double S = 0.0;
    for (int i = 0; i < 12; i++)
        S += (double)rand()/RAND_MAX;
    return S - 6.0;
}
